<?php

namespace app\admin\controller;

use app\common\model\Site;
use app\common\model\SiteCategory;
use app\common\controller\AdminBase;

/**
 * 站点分类管理
 * Class Category
 * @package app\admin\controller
 */
class SiteCate extends AdminBase
{

    protected $model;
    protected $site_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new SiteCategory();
        $this->site_model = new Site();
        $category_level_list = $this->model->catetree();
        $this->assign('category_level_list', $category_level_list);
    }

    /**
     * 站点分类管理
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 添加站点分类
     * @param string $pid
     * @return mixed
     */
    public function add($pid = '')
    {
        return $this->fetch('add');
    }

    /**
     * 保存站点分类
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $validateResult = $this->validate($data, 'SiteCate');
            if ($validateResult !== true) {

                return json(array('code' => 0, 'msg' => $validateResult, 'data' => []));

            } else {
                if ($this->model->allowField(true)->save($data)) {
                    return json(array('code' => 200, 'msg' => '添加成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '添加失败'));
                }
            }
        }
    }

    /**
     * 编辑站点分类
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {


        $category = $this->model->find($id);

        return $this->fetch('edit', ['tptc' => $category]);
    }

    /**
     * 更新站点分类
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $validateResult = $this->validate($data, 'SiteCate');
            if ($validateResult !== true) {

                return $this->error($validateResult);
            } else {

                if ($this->model->allowField(true)->save($data, $id) !== false) {
                    return json(array('code' => 200, 'msg' => '更新成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '更新失败'));
                }
            }
        }
    }

    public function toggle($id, $status, $name)
    {
        if ($this->request->isGet()) {

            if ($this->model->where('id', $id)->update([$name => $status]) !== false) {
                return json(array('code' => 200, 'msg' => '设置成功'));
            } else {
                return json(array('code' => 0, 'msg' => '设置失败'));
            }
        }

    }

    /**
     * 删除站点分类
     * @param $id
     */
    public function delete($id)
    {
        $category = $this->model->where(['parent_id' => $id])->find();
        $details = $this->site_model->where(['category_id' => $id])->find();

        if (!empty($category)) {
            return json(array('code' => 0, 'msg' => '此分类下存在子分类，不可删除。可先删除子分类再操作'));
        }
        if (!empty($details)) {
            return json(array('code' => 0, 'msg' => '此分类下存在网址数据，不可删除。可先删除该分类下的网址后再操作'));
        }
        if ($this->model->destroy($id)) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
}